﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace BpmConnection2Sec
{
    class Logger
    {
        static Logger instance = null;
        private StreamWriter w = null;
        private String log_file;
        

        private Logger() {
            
        }

        public static Logger getInstance()
        {
            
            if(instance==null){
                instance = new Logger();

            }
            return instance;
        }


        public void log(String row,String type)
        {
            try
            {
                
                switch(type){
                    case "STD":
                        this.w.WriteLine("{0}  -  [STD]" + row, DateTime.Now);
                        break;
                    case "ERR":
                        this.w.WriteLine("{0}  -  [ERR]" + row, DateTime.Now);
                        break;
                }
                w.Flush();
            }catch(Exception e){
                Console.WriteLine(e.Message);
                Environment.ExitCode = 1;
                //System.Windows.Forms.MessageBox.Show("Error with log file. " + e.Message, "APPLICATION ERROR", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);                
            }
        }

        public void logStartSession()
        {
            try
            {
                
                this.w.WriteLine(""); 
                this.w.WriteLine("");
                this.w.WriteLine("############################################################################################");
                this.w.WriteLine("#########################  Session Started at {0}  ##########################", DateTime.Now);
                this.w.WriteLine("");
                w.Flush();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Environment.ExitCode = 1;
                //System.Windows.Forms.MessageBox.Show("Error with log file: " + e.Message, "APPLICATION ERROR", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);                
            }
        }

        public void logEndSession()
        {
            try
            {
                
                this.w.WriteLine("");
                this.w.WriteLine("##########################  Session Closed at {0} ##########################", DateTime.Now);
                this.w.WriteLine("############################################################################################");
                w.Flush();
                
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Environment.ExitCode = 1;
                //System.Windows.Forms.MessageBox.Show("Error with log file:. " + e.Message, "APPLICATION ERROR", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);                
            }
        }

        public void setLogPath(String path)
        {
            this.log_file = path;
            this.w = File.AppendText(log_file);
        }
    }
}
